package com.wenan.ui.controls.dateChooser
{
	
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Grid;
	import mx.containers.GridItem;
	import mx.containers.GridRow;
	import mx.containers.HBox;
	
	import spark.components.Label;
	
	public class HighlightLabel extends BorderLabel
	{
		
		private var _customColor:uint = 0xFFFFFF;
		//选择
		private var _selectedColor:uint = 0xA8C6EE;
		private var _selectedBorder:uint = 0x7B99C1;
		//移入
		private var _hoverColor:uint = 0xCEDBEF;
		private var _hoverBorder:uint = 0x99A3AE;
		
		public var allowMultipleSelection:Boolean =	false;
		private var _selected:Boolean = false;
		
		public function HighlightLabel()
		{
			super();
			addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
			addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
			addEventListener(MouseEvent.CLICK, clickHandler);
		}
		
		public function get selected():Boolean
		{
			return _selected;
		}

		public function set selected(value:Boolean):void
		{
			_selected = value;
			
			if (_selected)
			{
				backgroundColor(_selectedColor);
				borderColor(_selectedBorder);
			}
			else
			{
				backgroundColor(_customColor);
				borderColor(_customColor);
			}
		}

		public function mouseOverHandler(event:MouseEvent):void
		{
			backgroundColor(_hoverColor);
			borderColor(_hoverBorder);
		}
		
		public function mouseOutHandler(event:MouseEvent):void
		{
			if (_selected)
			{
				backgroundColor(_selectedColor);
				borderColor(_selectedBorder);
			}
			else
			{
				var doc:DisplayObjectContainer = ((((event.currentTarget as HighlightLabel).owner as GridItem).owner as GridRow).owner as Grid).owner;
				if (doc is YearGrid){
					var yg:YearGrid = doc as YearGrid;
					var arr:ArrayCollection = yg.dateArray;
					for each (var hl:HighlightLabel in arr)
					{
						if (!hl.selected)
						{
							hl.removeStyle();
						}
					}
				} 
				else if (doc is MonthGrid) {
					var mg:MonthGrid = doc as MonthGrid;
					var dateArray:ArrayCollection = mg.dateArray;
					for each (var h:HighlightLabel in dateArray)
					{
						if (!h.selected)
						{
							h.removeStyle();
						}
					}
				}
			}
		}
		
		private function clickHandler(event:MouseEvent):void
		{
			if (allowMultipleSelection) {
				backgroundColor(_selectedColor);
				borderColor(_selectedBorder);
			} 
			else 
			{
				var doc:DisplayObjectContainer = ((((event.currentTarget as HighlightLabel).owner as GridItem).owner as GridRow).owner as Grid).owner;
				if (doc is YearGrid){
					var yg:YearGrid = doc as YearGrid;
					yg.cancleSelected();
				} 
				else if (doc is MonthGrid) {
					 var mg:MonthGrid = doc as MonthGrid;
					 mg.cancleSelected();
				}
			}
			selected = true;
			this.dispatchEvent(new DateEvent(DateEvent.SELECTEDDATE, _label.text));
		}
		
		public function removeStyle():void 
		{
			selected = false;
			backgroundColor(_customColor);
			borderColor(_customColor);
		}
	}
}